package com.baofu.paycore.service;

import com.baofu.paycore.common.enums.FundTypeEnum;
import com.baofu.paycore.common.utils.ExceptionUtil;
import com.baofu.paycore.dal.model.*;
import com.baofu.paycore.manager.model.PayBO;
import com.baofu.paycore.manager.model.SubPayBO;
import com.baofu.paycore.manager.storage.impl.*;
import com.baofu.paycore.service.converter.DOToDTOConverter;
import com.baofu.paycore.service.converter.DTO2BOConverter;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.baofu.paycore.service.facade.enums.PayStatusEnums;
import com.baofu.paycore.service.facade.enums.RequestSystemEnum;
import com.baofu.paycore.service.facade.enums.ServiceCodeEnum;
import com.baofu.paycore.common.exception.ManagerErrorCode;
import com.baofu.paycore.service.converter.BO2DTOConverter;
import com.baofu.paycore.service.facade.PayQueryServiceFacade;
import com.baofu.paycore.service.facade.model.*;
import com.baofu.paycore.service.facade.model.pay.FundDetailInfoRespDTO;
import com.baofu.paycore.service.facade.model.pay.PayQueryDetailRespDTO;
import com.baofu.paycore.service.facade.model.recharge.RechargeDetailRespDTO;
import com.baofu.paycore.service.facade.model.transfer.TransferDetailRespDTO;
import com.google.common.collect.Lists;
import com.system.commons.exception.BizServiceException;
import com.system.commons.result.PageReqDTO;
import com.system.commons.result.PageRespDTO;
import com.system.commons.result.Result;
import com.system.commons.utils.BeanCopyUtil;
import com.system.commons.utils.ExceptionUtils;
import com.system.commons.utils.ParamValidate;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.slf4j.helpers.SystemMarker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * description:支付交易查询
 * <p>
 * 1.根据支付单号查询交易
 * 2.根据订单号查询交易
 * 3.分页查询支付信息
 * 4.依支付单号查询支付明细信息
 * 5.依支付单号查询转账明细信息
 * 6.依支付单号查询充值/提现明细信息
 * 7.依条件查询支付明细信息
 * </p>
 * User: liu ting  Date: 2016/5/24 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Service
public class PayQueryService implements PayQueryServiceFacade {

    /**
     * 支付交易查询manager
     */
    @Autowired
    private PayManager payManager;

    /**
     * 充值提现manager
     */
    @Autowired
    private ClearManager clearManager;

    /**
     * 转账服务
     */
    @Autowired
    private TransferManager transferManager;

    /**
     * 支付子单服务
     */
    @Autowired
    private SubPayManager subPayManager;

    /**
     * 转账子单服务
     */
    @Autowired
    private SubTransferManager subTransferManager;

    /**
     * 充值银行单服务
     */
    @Autowired
    private ClearBankManager clearBankManager;

    /**
     * 冻结服务
     */
    @Autowired
    private FreezeManager freezeManager;

    /**
     * 充值账务单服务
     */
    @Autowired
    private ClearAccountManager clearAccountManager;

    /**
     * 预授权服务
     */
    @Autowired
    private PreAuthManager preAuthManager;

    /**
     * 预授权子单服务
     */
    @Autowired
    private SubPreAuthManager subPreAuthManager;

    /**
     * 退款主单处理服务
     */
    @Autowired
    private RefundManager refundManager;


    /**
     * 1.根据支付单号查询交易
     *
     * @param payNo      支付单号
     * @param traceLogId 日志ID
     * @return 交易对象
     */
    @Override
    public Result<PayResDTO> queryByPayNo(String payNo, String serviceCode, String traceLogId) {

        MDC.put(SystemMarker.TRACE_LOG_ID, traceLogId);
        log.info("根据支付单号查询交易传入参数, payNo:{}, serviceCode:{}", payNo, serviceCode);
        Result<PayResDTO> result;

        try {
            //校验参数
            ParamValidate.validateNull(payNo);
            ParamValidate.validateNull(serviceCode);
            //如果是消费类 则走下面方法
            if (serviceCode.trim().equals(ServiceCodeEnum.PAY.getCode())) {

                PayDO payDO = payManager.queryByPayNo(payNo);
                //转化对象
                PayResDTO resDTO = BeanCopyUtil.objConvert(payDO, PayResDTO.class);
                result = new Result<>(resDTO);
            } else if (serviceCode.trim().equals(ServiceCodeEnum.RECHARGE.getCode())
                    || serviceCode.trim().equals(ServiceCodeEnum.WITHDRAW.getCode())
                    || serviceCode.trim().equals(ServiceCodeEnum.OFFLINE_RECHARGE.getCode())) {//如果是充值提现类、则走下面方法

                ClearDO clearDO = clearManager.queryByPayNo(payNo);
                //转化对象
                PayResDTO resDTO = BeanCopyUtil.objConvert(clearDO, PayResDTO.class);
                result = new Result<>(resDTO);
            } else if (serviceCode.trim().equals(ServiceCodeEnum.TRANSFER.getCode())) {
                TransferDO transferDO = transferManager.getByPayNo(payNo);
                //转化对象
                PayResDTO resDTO = BeanCopyUtil.objConvert(transferDO, PayResDTO.class);
                result = new Result<>(resDTO);
            } else {
                result = new Result<>(ManagerErrorCode.NO_SERVICE_CODE.getErrorCode(),
                        ManagerErrorCode.NO_SERVICE_CODE.getErrorDesc());
            }
        } catch (Exception e) {
            log.error("根据支付单号查询交易异常：{}", e);
            result = ExceptionUtil.doQueryExceptionService(e, PayResDTO.class);
        }

        log.info("根据支付单号查询交易返回结果：{}", result);
        return result;
    }

    /**
     * 2.根据订单号查询交易
     *
     * @param queryReqDTO 条件对象
     * @param traceLogId  日志ID
     * @return 交易对象
     */
    @Override
    public Result<PayResDTO> queryByOrderNo(PayQueryReqDTO queryReqDTO, String traceLogId) {

        MDC.put(SystemMarker.TRACE_LOG_ID, traceLogId);
        log.info("根据订单号查询交易传入参数：{}", queryReqDTO);
        Result<PayResDTO> result;

        try {
            //校验对象
            ParamValidate.validateParams(queryReqDTO);
            String serviceCode = queryReqDTO.getServiceCode();
            log.info("serviceCode.trim().equals(ServiceCodeEnum.REFUND)="+(ServiceCodeEnum.REFUND.getCode().equals(serviceCode))+"," +
                    "ServiceCodeEnum.REFUND="+ServiceCodeEnum.REFUND.getCode());
            //如果是消费类  则走消费查询
            if (serviceCode.trim().equals(ServiceCodeEnum.PAY.getCode())) {
                //转化对象
                PayDO resPayDO = payManager.queryByOrderNo(queryReqDTO.getRequestOrderNo(), queryReqDTO.getRequestBizNo(),
                        queryReqDTO.getRequestDate(), queryReqDTO.getRequestSystem());
                //转化对象
                PayResDTO resDTO = BeanCopyUtil.objConvert(resPayDO, PayResDTO.class);
                resDTO.setRealRtnFlag(true);
                result = new Result<>(resDTO);
            } else if (serviceCode.trim().equals(ServiceCodeEnum.RECHARGE.getCode())
                    || serviceCode.trim().equals(ServiceCodeEnum.WITHDRAW.getCode())
                    || serviceCode.trim().equals(ServiceCodeEnum.UNFRE_WITHDRAW.getCode())
                    || serviceCode.trim().equals(ServiceCodeEnum.ISSUE_WITHDRAW.getCode())
                    || serviceCode.trim().equals(ServiceCodeEnum.OFFLINE_RECHARGE.getCode())) {//如果是充值提现类、则走下面方法
                //转化对象
                ClearDO clearResDO = clearManager.queryByOrderNo(queryReqDTO.getRequestOrderNo(),
                        queryReqDTO.getRequestBizNo(), queryReqDTO.getRequestDate(), queryReqDTO.getRequestSystem());
                //转化对象
                PayResDTO resDTO = BeanCopyUtil.objConvert(clearResDO, PayResDTO.class);
                resDTO.setRealRtnFlag(true);
                result = new Result<>(resDTO);
            } else if (serviceCode.trim().equals(ServiceCodeEnum.TRANSFER.getCode()) ||
                    serviceCode.trim().equals(ServiceCodeEnum.UNFRE_TRANSFER.getCode()) ||
                    serviceCode.trim().equals(ServiceCodeEnum.CANCEL.getCode())) {
                TransferDO transferDO = transferManager.selectByOrderNoAndBizNo(queryReqDTO.getRequestOrderNo(),
                        queryReqDTO.getRequestBizNo(), queryReqDTO.getRequestSystem(), queryReqDTO.getRequestDate());
                PayResDTO payResDTO = BO2DTOConverter.transferToPayResDTO(transferDO);
                payResDTO.setRealRtnFlag(true);
                result = new Result<>(payResDTO);
            } else if (serviceCode.trim().equals(ServiceCodeEnum.AUTH.getCode()) || serviceCode.trim().equals(ServiceCodeEnum.AUTH_CANCEL.getCode())
                    || serviceCode.trim().equals(ServiceCodeEnum.AUTH_CONFIRM.getCode())) {
                PreAuthDO preAuthDO = preAuthManager.selectByOrderNoAndBizNo(queryReqDTO.getRequestOrderNo(),
                        queryReqDTO.getRequestBizNo(), queryReqDTO.getRequestSystem(), queryReqDTO.getRequestDate());
                PayResDTO payResDTO = BO2DTOConverter.preAuthToPayResDTO(preAuthDO);
                payResDTO.setRealRtnFlag(true);
                if (PayStatusEnums.SUCCESS.getCode().equals(preAuthDO.getStatus()) &&
                        serviceCode.trim().equals(ServiceCodeEnum.AUTH.getCode())) {
                    payResDTO.setFreezeCode(subPreAuthManager.selectSubPreAuthByPayNo(preAuthDO.getPayNo(), FundTypeEnum.PRE_AUTH.getCode()).getFreezeCode());
                }
                result = new Result<>(payResDTO);
            }else if(serviceCode.trim().equals(ServiceCodeEnum.REFUND.getCode())){//退款
                //转化对象
                RefundDO refundDO = refundManager.queryByOrderNo(queryReqDTO.getRequestOrderNo(),
                        queryReqDTO.getRequestBizNo(), queryReqDTO.getRequestDate(), queryReqDTO.getRequestSystem());
                //转化对象
                PayResDTO resDTO = BeanCopyUtil.objConvert(refundDO, PayResDTO.class);
                resDTO.setRealRtnFlag(true);
                result = new Result<>(resDTO);
            }else if(serviceCode.trim().equals(ServiceCodeEnum.FREEZE.getCode()) ||
                    serviceCode.trim().equals(ServiceCodeEnum.UNFREEZE.getCode())){
                FreezeDO freezeDO=freezeManager.selectByOrderNoAndBizNo(queryReqDTO.getRequestOrderNo(),
                        queryReqDTO.getRequestBizNo(), queryReqDTO.getRequestSystem(), queryReqDTO.getRequestDate());
                PayResDTO payResDTO = BO2DTOConverter.freezeToPayResDTO(freezeDO);
                payResDTO.setRealRtnFlag(true);
                if (PayStatusEnums.SUCCESS.getCode().equals(freezeDO.getStatus()) &&
                        serviceCode.trim().equals(ServiceCodeEnum.FREEZE.getCode())) {
                    payResDTO.setFreezeCode(freezeDO.getFreezeCode());
                }
                result = new Result<>(payResDTO);
            } else {
                result = new Result<>(ManagerErrorCode.NO_SERVICE_CODE.getErrorCode(),
                        ManagerErrorCode.NO_SERVICE_CODE.getErrorDesc());
            }
    }catch(Exception e)

    {
        log.error("根据订单号查询交易异常：{}", e);
        result = ExceptionUtil.doQueryExceptionService(e, PayResDTO.class);
    }
        log.info("根据订单号查询交易返回结果：{}",result);
        return result;
}

    /**
     * 分页查询支付信息
     *
     * @param payPageQueryReqDTO 查询请求参数
     * @param pageReqDTO         分页查询参数
     * @param traceLogId         日志ID
     * @return 支付信息
     */
    @Override
    public Result<PageRespDTO<PayQueryRespDTO>> queryPayInfoByPage(PayPageQueryReqDTO payPageQueryReqDTO,
                                                                   PageReqDTO pageReqDTO, String traceLogId) {
        MDC.put(SystemMarker.TRACE_LOG_ID, traceLogId);
        log.info("分页查询支付信息传入参数：{},pageReqDTO:{}", payPageQueryReqDTO, pageReqDTO);
        Result<PageRespDTO<PayQueryRespDTO>> result;
        try {
            //校验对象
            ParamValidate.validateParams(payPageQueryReqDTO);
            ParamValidate.validateNull(payPageQueryReqDTO.getStartDate(), PayCoreErrorCode.REQUEST_START_DATE);
            ParamValidate.validateNull(payPageQueryReqDTO.getEndDate(), PayCoreErrorCode.REQUEST_END_DATE);
            PaycorePageDTO paycorePageDTO = new PaycorePageDTO();
            paycorePageDTO.setStartDate(payPageQueryReqDTO.getStartDate());
            paycorePageDTO.setEndDate(payPageQueryReqDTO.getEndDate());
            paycorePageDTO.setCurrentPage(pageReqDTO.getCurrentPage());
            paycorePageDTO.setPageCount(pageReqDTO.getPageCount());
            ServiceCodeEnum serviceCodeEnum = ServiceCodeEnum.explain(payPageQueryReqDTO.getServiceCode());
            if (serviceCodeEnum == null) {
                log.error("分页查询支付信息 服务编号:{}", payPageQueryReqDTO.getServiceCode());
                throw new BizServiceException(PayCoreErrorCode.VALUE_NOT_SUPPORT, "不支持此服务编号");
            }
            switch (serviceCodeEnum) {
                case PAY:
                    PayBO payBO = DTO2BOConverter.getPayBOByPayPageQueryReqDTO(payPageQueryReqDTO);
                    int count = payManager.queryPayCount(payBO, paycorePageDTO);
                    List<PayDO> list = payManager.queryPayPage(payBO, paycorePageDTO, pageReqDTO.getStartRow(),
                            pageReqDTO.getPageCount());
                    result = new Result<>(new PageRespDTO<>(count, pageReqDTO.getPageCount(),
                            BO2DTOConverter.getPayResDTOByPayDO(list)));
                    break;
                case RECHARGE:
                case OFFLINE_RECHARGE:
                case WITHDRAW:
                    ClearDO clearDO = DTO2BOConverter.getClearDOByPayPageQueryReqDTO(payPageQueryReqDTO);
                    int rechargeCount = clearManager.queryClearCount(clearDO, paycorePageDTO);
                    List<ClearDO> clearDOs = clearManager.queryClearPage(clearDO, paycorePageDTO);
                    result = new Result<>(new PageRespDTO<>(rechargeCount, pageReqDTO.getPageCount(),
                            BO2DTOConverter.getPayResDTOByClearDO(clearDOs)));
                    break;
                case TRANSFER:
                    TransferDO transferDO = DTO2BOConverter.getTransferDOByPayPageQueryReqDTO(payPageQueryReqDTO);
                    int transferCount = transferManager.selectTransferCount(transferDO, paycorePageDTO);
                    List<TransferDO> transferDOs = transferManager.selectTransferPage(transferDO,
                            paycorePageDTO);
                    result = new Result<>(new PageRespDTO<>(transferCount, pageReqDTO.getPageCount(),
                            BO2DTOConverter.getPayResDTOByTransferDO(transferDOs)));
                    break;
                default:
                    log.error("分页查询支付信息 服务编号:{}", payPageQueryReqDTO.getServiceCode());
                    throw new BizServiceException(PayCoreErrorCode.VALUE_NOT_SUPPORT, "暂不支持此服务查询");
            }
        } catch (Exception e) {
            log.error("分页查询支付信息异常：{}", e);
            result = ExceptionUtils.getResponse(e);
        }
        log.info("分页查询支付信息返回结果：{}", result);
        return result;
    }

    /**
     * 依支付单号查询支付明细信息
     *
     * @param payNo      支付单号
     * @param traceLogId 日志ID
     * @return 支付明细信息
     */
    @Override
    public Result<PayQueryDetailRespDTO> queryPayDetailByPayNo(String payNo, String traceLogId) {

        MDC.put(SystemMarker.TRACE_LOG_ID, traceLogId);
        log.info("依支付单号查询支付明细信息 payNo：{}", payNo);
        Result<PayQueryDetailRespDTO> result;
        try {
            //校验参数
            ParamValidate.validateNull(payNo);
            //主单信息
            PayDO payDO = payManager.queryByPayNo(payNo);
            //子单信息
            List<SubPayDO> subPayDOs = subPayManager.selectSubPay(payNo);
            result = new Result<>(DOToDTOConverter.buildPayQueryDetailRespDTO(payDO, subPayDOs));
        } catch (Exception e) {
            log.error("依支付单号查询支付明细信息异常：{}", e);
            result = ExceptionUtils.getResponse(e);
        }
        log.info("依支付单号查询支付明细信息返回结果：{}", result);
        return result;
    }

    /**
     * 依支付单号查询转账明细信息
     *
     * @param payQueryReqDTO 查询请求参数
     * @param traceLogId     日志ID
     * @return 转账明细信息
     */
    @Override
    public Result<TransferDetailRespDTO> queryTransferByPayNo(PayQueryReqDTO payQueryReqDTO, String traceLogId) {

        MDC.put(SystemMarker.TRACE_LOG_ID, traceLogId);
        log.info("依条件查询转账单信息 请求参数：{}", payQueryReqDTO);
        Result<TransferDetailRespDTO> result;
        try {
            //校验参数
            ParamValidate.validateParams(payQueryReqDTO);
            if (!ServiceCodeEnum.TRANSFER.getCode().equals(payQueryReqDTO.getServiceCode())) {
                log.error("依条件查询转账单信息, 请求服务编号:{}", payQueryReqDTO.getServiceCode());
                throw new BizServiceException(PayCoreErrorCode.VALUE_NOT_SUPPORT, "此服务不支付除转账单外查询信息");
            }
            //主单信息
            TransferDO transferDO = transferManager.selectByOrderNoAndBizNo(payQueryReqDTO.getRequestOrderNo(),
                    payQueryReqDTO.getRequestBizNo(), payQueryReqDTO.getRequestSystem(), null);
            //子单信息
            List<SubTransferDO> subTransferDOs = subTransferManager.selectByPayNo(transferDO.getPayNo());
            result = new Result<>(DOToDTOConverter.buildTransferDetailRespDTO(transferDO, subTransferDOs));
        } catch (Exception e) {
            log.error("依条件查询转账单信息,异常：{}", e);
            result = ExceptionUtils.getResponse(e);
        }
        log.info("依条件查询转账单信息,返回结果：{}", result);
        return result;
    }

    /**
     * 依支付单号查询充值/提现明细信息
     *
     * @param payQueryReqDTO 支付单号
     * @param traceLogId     日志ID
     * @return 充值/提现明细信息
     */
    @Override
    public Result<RechargeDetailRespDTO> queryRechargeByPayNo(PayQueryReqDTO payQueryReqDTO, String traceLogId) {

        MDC.put(SystemMarker.TRACE_LOG_ID, traceLogId);
        log.info("依条件查询[充值/线下充值/提现]交易信息 请求参数：{}", payQueryReqDTO);
        Result<RechargeDetailRespDTO> result;
        try {
            //校验参数
            ParamValidate.validateParams(payQueryReqDTO);
            ServiceCodeEnum serviceCodeEnum = ServiceCodeEnum.explain(payQueryReqDTO.getServiceCode());
            if (serviceCodeEnum == null) {
                log.error("依条件查询[充值/线下充值/提现]交易信息 服务编号:{}", payQueryReqDTO.getServiceCode());
                throw new BizServiceException(PayCoreErrorCode.VALUE_NOT_SUPPORT, "不支持此服务编号");
            }
            switch (serviceCodeEnum) {
                case RECHARGE:
                case OFFLINE_RECHARGE:
                case WITHDRAW:
                    //主单信息
                    ClearDO clearDO = clearManager.queryByOrderNo(payQueryReqDTO.getRequestOrderNo(),
                            payQueryReqDTO.getRequestBizNo(), payQueryReqDTO.getRequestDate(),
                            payQueryReqDTO.getRequestSystem());
                    //子单信息
                    ClearBankDO clearBankDO = clearBankManager.queryClearBankInfo(clearDO.getPayNo());
                    //冻结单信息
                    FreezeDO freezeDO = freezeManager.selectByPayNo(clearDO.getPayNo());
                    List<ClearAccountDO> clearAccountDOs =
                            clearAccountManager.queryClearAccountByPayNo(clearDO.getPayNo());
                    result = new Result<>(DOToDTOConverter.buildRechargeDetailRespDTO(clearDO, clearAccountDOs,
                            clearBankDO, freezeDO));
                    break;
                default:
                    log.error("依条件查询[充值/线下充值/提现]交易信息 服务编号:{}", payQueryReqDTO.getServiceCode());
                    throw new BizServiceException(PayCoreErrorCode.VALUE_NOT_SUPPORT,
                            "暂不支持此非[充值/线下充值/提现]交易查询");
            }

        } catch (Exception e) {
            log.error("依条件查询[充值/线下充值/提现]交易信息, 异常：{}", e);
            result = ExceptionUtils.getResponse(e);
        }
        log.info("依条件查询[充值/线下充值/提现]交易信息, 返回结果：{}", result);
        return result;
    }

    /**
     * 依条件查询支付明细信息
     *
     * @param payQueryReqDTO 查询参数条件
     * @param traceLogId     日志ID
     * @return 支付明细信息
     */
    @Override
    public Result<PayQueryDetailRespDTO> queryPayDetailInfoByPay(PayQueryReqDTO payQueryReqDTO, String traceLogId) {

        MDC.put(SystemMarker.TRACE_LOG_ID, traceLogId);
        log.info("依条件查询[支付]交易信息,传入参数：{}", payQueryReqDTO);

        Result<PayQueryDetailRespDTO> result;
        try {
            //校验对象
            ParamValidate.validateParams(payQueryReqDTO);
            String serviceCode = payQueryReqDTO.getServiceCode();
            ServiceCodeEnum serviceCodeEnum = ServiceCodeEnum.explain(serviceCode);
            if (serviceCodeEnum == null) {
                log.error("依条件查询[支付]交易信息 服务编号:{}", serviceCode);
                throw new BizServiceException(PayCoreErrorCode.VALUE_NOT_SUPPORT, "不支持此服务编号");
            }
            switch (serviceCodeEnum) {
                case PAY:
                    PayBO payBO = payManager.queryPayInfo(payQueryReqDTO.getRequestOrderNo(),
                            payQueryReqDTO.getRequestBizNo(), payQueryReqDTO.getRequestDate(),
                            payQueryReqDTO.getRequestSystem());
                    List<SubPayBO> subPayBOs = payBO.getSubPayList();
                    ClearDO clearDO;
                    ClearBankDO clearBankDO;
                    List<ClearAccountDO> clearAccountDOs;
                    List<FundDetailInfoRespDTO> list = Lists.newArrayList();
                    for (SubPayBO subPayBO : subPayBOs) {
                        if (FundTypeEnum.PAY.getCode().equals(subPayBO.getFundType())) {
                            clearDO = clearManager.queryByOrderNo(payBO.getPayNo(), subPayBO.getFundDetailNo(),
                                    null, RequestSystemEnum.PAYCORE.getCode());
                            clearBankDO = clearBankManager.queryClearBankInfo(clearDO.getPayNo());
                            clearAccountDOs =
                                    clearAccountManager.queryClearAccountByPayNo(clearDO.getPayNo());
                            list.add(BO2DTOConverter.getFundDetailInfoRespDTO(subPayBO, clearDO, clearBankDO,
                                    clearAccountDOs));
                        }
                    }
                    PayQueryDetailRespDTO payQueryDetailRespDTO = new PayQueryDetailRespDTO();
                    BeanCopyUtil.copy(payBO, payQueryDetailRespDTO);
                    payQueryDetailRespDTO.setFundDetailInfoRespDTOs(list);

                    result = new Result<>(payQueryDetailRespDTO);
                    break;
                default:
                    log.error("依条件查询[支付]交易信息 服务编号:{}", payQueryReqDTO.getServiceCode());
                    throw new BizServiceException(PayCoreErrorCode.VALUE_NOT_SUPPORT, "暂不支持此非[支付]交易查询");
            }

        } catch (Exception e) {
            log.error("依条件查询[支付]交易信息,异常：{}", e);
            result = ExceptionUtils.getResponse(e);
        }
        log.info("依条件查询[支付]交易信息,返回结果：{}", result);
        return result;
    }

    /**
     * 依条件分页查询支付订单差异状态信息
     *
     * @param payDiffOrderReqDTO 查询参数条件
     * @param traceLogId         日志ID
     * @return 支付单信息
     */
    @Override
    public Result<PageRespDTO<PayDiffOrderRespDTO>> queryPayDiffOrderByParam(PayDiffOrderReqDTO payDiffOrderReqDTO,
                                                                             String traceLogId) {
        MDC.put(SystemMarker.TRACE_LOG_ID, traceLogId);
        log.info("依条件分页查询支付订单差异状态信息,传入参数：{}", payDiffOrderReqDTO);
        Result<PageRespDTO<PayDiffOrderRespDTO>> result;
        try {
            //校验对象
            ParamValidate.validateParams(payDiffOrderReqDTO);
            ParamValidate.validateNull(payDiffOrderReqDTO.getStartDate(), PayCoreErrorCode.REQUEST_START_DATE);
            ParamValidate.validateNull(payDiffOrderReqDTO.getEndDate(), PayCoreErrorCode.REQUEST_END_DATE);
            ClearDiffDO clearDiffDO = BeanCopyUtil.objConvert(payDiffOrderReqDTO, ClearDiffDO.class);
            int count = clearManager.queryPayDiffOrderCount(clearDiffDO);
            List<ClearDiffDO> list = clearManager.queryPayDiffOrderPage(clearDiffDO, payDiffOrderReqDTO.getStartRow(),
                    payDiffOrderReqDTO.getPageCount());
            result = new Result<>(new PageRespDTO<>(count, payDiffOrderReqDTO.getPageCount(),
                    BeanCopyUtil.convertList(list, PayDiffOrderRespDTO.class)));
        } catch (Exception e) {
            log.error("依条件分页查询支付订单差异状态信息,异常：{}", e);
            result = ExceptionUtils.getResponse(e);
        }
        log.info("依条件分页查询支付订单差异状态信息,返回结果：{}", result);
        return result;
    }

    /**
     * 依支付单号查询账务信息
     *
     * @param payNo      支付单号
     * @param traceLogId 日志ID
     * @return 支付明细信息
     */
    @Override
    public Result<List<ClearAccountResDTO>> queryAccountDetailByPayNo(String payNo, String traceLogId) {

        MDC.put(SystemMarker.TRACE_LOG_ID, traceLogId);
        log.info("依支付单号查询支付明细信息 payNo：{}", payNo);
        Result<List<ClearAccountResDTO>> result;
        try {
            //校验参数
            ParamValidate.validateNull(payNo);
            //主单信息
            List<ClearAccountDO> clearAccountDOS = clearAccountManager.queryClearAccountByPayNo(payNo);
            result = new Result<>(BeanCopyUtil.convertList(clearAccountDOS,ClearAccountResDTO.class));
        } catch (Exception e) {
            log.error("依支付单号查询支付明细信息异常：{}", e);
            result = ExceptionUtils.getResponse(e);
        }
        log.info("依支付单号查询支付明细信息返回结果：{}", result);
        return result;
    }


    /**
     *依支付单号查询账务信息
     * @param payNo      支付单号
     * @param serviceCode 服务编码
     * @param traceLogId 日志ID
     * @return          Result<List<ClearAccountResDTO>>
     */
    @Override
    public Result<List<ClearAccountResDTO>> queryAccountDetailByPayNo(String payNo, String serviceCode, String traceLogId) {
        MDC.put(SystemMarker.TRACE_LOG_ID, traceLogId);
        log.info("依支付单号查询支付明细信息 payNo：{}，serviceCode", payNo,serviceCode);
        Result<List<ClearAccountResDTO>> result;
        try {
            //校验参数
            ParamValidate.validateNull(payNo);
            ParamValidate.validateNull(serviceCode);
            ServiceCodeEnum serviceCodeEnum = ServiceCodeEnum.explain(serviceCode);
            if (serviceCodeEnum == null) {
                log.error("依条件查询[支付]交易信息 服务编号:{}", serviceCode);
                throw new BizServiceException(PayCoreErrorCode.VALUE_NOT_SUPPORT, "不支持此服务编号");
            }
            switch (serviceCodeEnum) {
                case RECHARGE:
                case OFFLINE_RECHARGE:
                case WITHDRAW:
                    //主单信息
                    List<ClearAccountDO> clearAccountDOS = clearAccountManager.queryClearAccountByPayNo(payNo);
                    result = new Result<>(BeanCopyUtil.convertList(clearAccountDOS,ClearAccountResDTO.class));
                    break;
                case TRANSFER:
                    List<SubTransferDO> subTransferDOS=subTransferManager.selectByPayNo(payNo);
                    result = new Result<>(BeanCopyUtil.convertList(subTransferDOS,ClearAccountResDTO.class));
                    break;
                default:
                    log.error("分页查询支付信息 服务编号:{}", serviceCode);
                    throw new BizServiceException(PayCoreErrorCode.VALUE_NOT_SUPPORT, "暂不支持此服务查询");
            }
        } catch (Exception e) {
            log.error("依支付单号查询支付明细信息异常：{}", e);
            result = ExceptionUtils.getResponse(e);
        }
        log.info("依支付单号查询支付明细信息返回结果：{}", result);
        return result;    }
}
